package org.itboys.shop.manager;

import org.itboys.shop.entity.ShopSeller;
import org.itboys.shop.service.ShopSellerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员和店铺之间的关系
 * @author weisky
 *
 * 2016年4月12日
 */
@Service
public class SellerMemberService {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ShopSellerService shopSellerService;
	
	/**
	 * 创建一个店铺的同时，为他生成一个会员；这个会员代表着这个店铺  方便后面的佣金计算
	 * @param seller
	 * @param logos
	 * @param mainImgs
	 * @return
	 */
	public boolean addSellerAndCreateMember(ShopSeller seller, MultipartFile logos, MultipartFile mainImgs,
                                            MultipartFile personCarImgs, MultipartFile businessLicenseImgs,
                                            MultipartFile foodLicenseImgs) {
		long tempSellerId = 0l;
		try{
			boolean resu = shopSellerService.insert(seller, logos, mainImgs, personCarImgs,
                    businessLicenseImgs, foodLicenseImgs);
			if(!resu){//对店铺的操作失败
				return false;
			}
			tempSellerId = seller.getId();
			/*if(tempSellerId == 0){//新增一个店铺
				//先判断数据库中是否已经存在这个会员  若没有这个会员  再添加
				//一个微信账号只能生成一个会员类型是99的店铺会员
				Map<String, Object> sellerMember = Maps.newHashMapWithExpectedSize(2);
				sellerMember.put("weixinId", seller.getWeixinId());
				sellerMember.put("type", FuzionConstants.Seller.SELLER_SELF_MEMBER);
				List<FuzionMember> fm = fuzionMemberService.list(sellerMember);
				if(fm.isEmpty()){
					FuzionMember fmember = new FuzionMember();
					fmember.setLogo(seller.getLogo());
					fmember.setNickname(seller.getName());
					fmember.setUsername(seller.getName());
					fmember.setRecLevel(FuzionConstants.TYPE_ZERO);
					fmember.setSellerId(seller.getId());
					fmember.setType(FuzionConstants.Seller.SELLER_SELF_MEMBER);
					fmember.setWeixinId(seller.getWeixinId() != null?seller.getWeixinId().toString() : "");
					fuzionMemberService.save(fmember);
					fmember.setPath(FuzionConstants.Seller.SELLER_MEMBER_PATH + fmember.getId() + FuzionConstants.Seller.SELLER_MEMBER_PATH );
					fmember.setRecId(fmember.getId());//推荐人是他自己
					fuzionMemberService.update(fmember);
					logger.info("创建店铺自己的会员成功：FuzionMember=" + JSON.toJSONString(fmember));
					
					//初始化店铺的会员收益信息
					//区分店铺会员和普通会员的区别：  店铺会员：memberId=0&&sellerId!=0  普通会员：memberId！=0&&sellerId!=0
					FuziongMemberStatistics fms = new FuziongMemberStatistics();
					fms.setMemberId(0L);
					fms.setScoreId(0L);
					fms.setTotalMoney(0);
					fms.setWithdrawTotalMoney(0);
					fms.setNowTotalMoney(0);
					fms.setEnergyBean(0);
					fms.setBuyCount(0);
					fms.setNowEnergyBean(0);
					fms.setDynamicIncome(0);
					fms.setStaticIncome(0);
					fms.setSellerId(fmember.getSellerId());
					fms.setPlayNum(0);
					fuziongMemberStatisticsService.insert(fms);
				}
				
			}*/
			return true;
		}catch(Exception e){
			e.printStackTrace();
			if(tempSellerId == 0){
				shopSellerService.dele(seller.getId());//如果创建店铺自己的会员失败，就把刚刚新增的店铺信息删除
			}
			logger.error("SellerMemberService.addSellerAndCreateMember err");
		}
		return false;
	}
}
